<?php
/**
 * Created by PhpStorm
 * User: Jhinwy
 * Date: 7/24/24
 * Time: 3:23 PM
 */

namespace app\adminapi\lists\return_rule;


use app\adminapi\lists\BaseAdminDataLists;
use app\adminapi\logic\auth\AdminLogic;
use app\common\model\return_rule\ReturnRule;
use app\common\lists\ListsSearchInterface;


/**
 * ReturnRule列表
 * Class ReturnRuleLists
 * @package app\adminapi\lists
 */
class ReturnRuleLists extends BaseAdminDataLists implements ListsSearchInterface
{


    /**
     * @notes 设置搜索条件
     * @return \string[][]
     * @author likeadmin
     * @date 2024/03/06 10:32
     */
    public function setSearch(): array
    {
        return [
            '%like%' => ['name'],
            '='      => ['id', 'promotion_link_id', 'app_id'],
        ];
    }


    /**
     * @notes 获取列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author likeadmin
     * @date 2024/03/06 10:32
     */
    public function lists(): array
    {
        return ReturnRule::where($this->searchWhere)->where($this->queryWhere())
            ->field(['id', 'admin_id', 'promotion_link_id', 'app_id', 'name', 'date_pattern', 'type', 'time', 'status', 'day', 'hour'])
            ->append(['app_name', 'admin_name', 'date_pattern_text'])
            ->hidden(['admin_id', 'date_pattern', 'day', 'hour'])
            ->limit($this->limitOffset, $this->limitLength)
            ->order(['id' => 'desc'])
            ->select()
            ->toArray();
    }

    /**
     * @notes 搜索条件
     * @author 段誉
     * @date 2023/2/24 16:08
     */
    public function queryWhere()
    {
        $where = [];

        if ($this->adminInfo['root'] != 1) {
            $pAdminIds = AdminLogic::getPermissionAdminIds($this->adminInfo);
            $where[]   = ['admin_id', 'in', $pAdminIds];
        }

        return $where;
    }


    /**
     * @notes 获取数量
     * @return int
     * @author likeadmin
     * @date 2024/03/06 10:32
     */
    public function count(): int
    {
        return ReturnRule::where($this->searchWhere)->where($this->queryWhere())->count();
    }

}
